// pages/prodClassify/prodClassify.js
import { request } from '../../utils/http'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        sts: 0,
        prodList: [],
        title:null,
        current: null,
        size: 10,
        pages: null,
        tagid: null,
    },
// 初始化
Init(){
    if (this.data.sts == 0) {
        if (this.data.tagid == 1) {
          wx.setNavigationBarTitle({
            title: '每日上新'
          })
        } else if (this.data.tagid == 2) {
          wx.setNavigationBarTitle({
            title: '商城热卖'
          })
        } 
      } else if (this.data.sts == 1) {
        wx.setNavigationBarTitle({
          title: '新品推荐',
        })
      }  else if (this.data.sts == 3) {
        wx.setNavigationBarTitle({
          title: '每日疯抢',
        })
      } else {
        wx.setNavigationBarTitle({
          title: this.data.title
        })
      }
},
// 初始化商品数据
loadProdData(){
    let sts = this.data.sts
    if (sts == 0) {
      // 分组标签商品列表
      this.getTagProd();
    } else if (sts == 1) {
      // 新品推荐
      let url = "/prod/lastedProdPage"
      this.getActProd(url)
    } else if (sts == 3) {
      // 每日疯抢
      let url = "/prod/moreBuyProdList"
      this.getActProd(url)
    } 
},
//新品推荐  每日疯抢
getActProd(url){
    request({
        url: url,
        data: {
            current: this.data.current,
            size: this.data.size,
          },
    }).then(res => {
        this.setData({
            prodList:res.records,
            pages:res.pages
        })
    })
},
//每日上新  商城热卖
getTagProd(){
    request({
        url: "/prod/prodListByTagId",
        data: {
            tagId: this.data.tagid,
            current: this.data.current,
            size: this.data.size,
          },
    }).then(res => {
        
        this.setData({
            prodList:res.records,
            pages:res.pages
        })
    })
},
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            current: 1,
            pages: 0,
            sts: options.sts,
            title: options.title ? options.title : ""
          });
          if (options.tagid) {
            this.setData({
              tagid: options.tagid
            });
          }
          // 初始化
          this.Init()
          //商品数据
          this.loadProdData()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (this.data.current < this.data.pages) {
            this.setData({
              current: this.data.current + 1
            })
           if(this.data.sts==1){
            request({
                url: "/prod/lastedProdPage",
                data: {
                    current: this.data.current,
                    size: this.data.size,
                  },
            }).then(res => {
                const prods = this.data.prodList.concat(res.records)
                this.setData({
                    prodList:prods,
                    
                })
            })
           }
          else if(this.data.sts==3){
            request({
                url: "/prod/moreBuyProdList",
                data: {
                    current: this.data.current,
                    size: this.data.size,
                  },
            }).then(res => {
                const prods = this.data.prodList.concat(res.records)
                this.setData({
                    prodList:prods,
                    
                })
            })
           }
           else if(this.data.sts==0){
            request({
                url: "/prod/prodListByTagId",
                data: {
                    tagId: this.data.tagid,
                    current: this.data.current,
                    size: this.data.size,
                  },
            }).then(res => {
               
                const prods = this.data.prodList.concat(res.records)
                this.setData({
                    prodList:prods,
                    
                })
            })
           }
          }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})